package com.ruoyi.project.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.travel.domain.User;
import com.ruoyi.project.travel.service.IUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户 Controller
 *
 * @author HYD
 * @date 2020-06-14
 */
@RestController
@RequestMapping("/travel/user" )
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 首页统计
     */
    @Log(title = "首页统计")
    @GetMapping("/tongji")
    public AjaxResult tongji() {
        return AjaxResult.success(userService.tongji());
    }

    /**
     * 查询用户 列表
     */
    @PreAuthorize("@ss.hasPermi('travel:user:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(User user) {
        startPage();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(user.getUserName() != null && user.getUserName() != ""){
            queryWrapper.like("user_name",user.getUserName());
        }
        if(user.getMobile() != null && user.getMobile() != ""){
            queryWrapper.like("mobile",user.getMobile());
        }
        List<User> list = userService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出用户 列表
     */
    @PreAuthorize("@ss.hasPermi('travel:user:export')" )
    @Log(title = "用户 " , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        List<User> list = userService.list(queryWrapper);
        ExcelUtil<User> util = new ExcelUtil<User>(User. class);
        return util.exportExcel(list, "user" );
    }

    /**
     * 获取用户 详细信息
     */
    @PreAuthorize("@ss.hasPermi('travel:user:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(userService.getById(id));
    }

    /**
     * 新增用户 
     */
    @PreAuthorize("@ss.hasPermi('travel:user:add')" )
    @Log(title = "用户 " , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.save(user) ? 1 : 0);
    }


    /**
     * 修改用户 
     */
    @PreAuthorize("@ss.hasPermi('travel:user:edit')" )
    @Log(title = "用户 " , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateById(user) ? 1 : 0);
    }

    /**
     * 删除用户 
     */
    @PreAuthorize("@ss.hasPermi('travel:user:remove')" )
    @Log(title = "用户 " , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


}

